//
//  PyramidStack.m
//  PezMonkey
//
//  Created by Spence DiNicolantonio on 11/13/09.
//  Copyright 2009 Necrosoft. All rights reserved.
//

#import "PyramidStack.h"


@implementation PyramidStack

- (NSString *)demoName {
	return @"Pyramid Stack";
}

/**
 * Initializes the receiver.
 */
- (void)start {
	// create space
	self.space = [PMSpace spaceWithGravity:PMVectMake(0, -1)];
	[space setSubSteps:3];
	[space setIterations:18];
	[space setActiveHashSize:1000 cellDimension:40];
	[space setStaticHashSize:1000 cellDimension:40];
	
	PMBody *body;
	PMShape *shape;
	
	// define block vertices
	NSUInteger blockVertCount = 4;
	PMVect blockVerts[] = {
		PMVectMake(-15,-15),
		PMVectMake(-15, 15),
		PMVectMake( 15, 15),
		PMVectMake( 15,-15),
	};
	
	
	// define boundary segment width 
	// (needs to be thick to avoid penetration at high velocities)
	PMFloat width = 30;
	
	// create shared static body
	PMBody *staticBody= [PMBody staticBody];
	
	// create left screen boundary
	shape = [PMSegmentShape segmentShapeWithBody:staticBody
										   start:PMVectMake(-320 - width/2, -240)
											 end:PMVectMake(-320 - width/2, 240)
										   width:width];
	[shape setElasticity:1.0];
	[shape setFriction:1.0];
	[shape setLayers:PMLayerMaskNotGrabbable];
	[space addStaticShape:shape];
	
	// create right screen boundary
	shape = [PMSegmentShape segmentShapeWithBody:staticBody
										   start:PMVectMake(320 + width/2, -240) 
											 end:PMVectMake(320 + width/2, 240)
										   width:width];
	[shape setElasticity:1.0];
	[shape setFriction:1.0];
	[shape setLayers:PMLayerMaskNotGrabbable];
	[space addStaticShape:shape];
	
	// create bottom screen boundary
	shape = [PMSegmentShape segmentShapeWithBody:staticBody
										   start:PMVectMake(-320, -240 - width/2) 
											 end:PMVectMake(320, -240 - width/2)
										   width:width];
	[shape setElasticity:1.0];
	[shape setFriction:1.0];
	[shape setLayers:PMLayerMaskNotGrabbable];
	[space addStaticShape:shape];
	
	
	// add blocks.
	for (int i=0; i<14; i++) {
		for(int j=0; j<=i; j++) {
			// body
			PMFloat moment = PMMomentForPolygon(1.0, blockVerts, blockVertCount, PMVectZero);
			body = [PMBody bodyWithMass:1 
								 moment:moment];
			[body setPosition:PMVectMake(j*32 - i*16, 
										 300 - i*32)];
			[space addBody:body];
			
			// shape
			shape = [PMPolygonShape polygonShapeWithBody:body 
												vertices:blockVerts
											 vertCount:blockVertCount];
			[shape setElasticity:0.0];
			[shape setFriction:0.8];
			[space addShape:shape];
		}
	}
	
	// add a ball to make things more interesting
	PMFloat radius = 15.0;
	PMFloat mass = 10.0;
	PMFloat moment = PMMomentForCircle(mass, 0.0, radius, PMVectZero);
	
	// body
	body = [PMBody bodyWithMass:mass 
						 moment:moment];
	[body setPosition:PMVectMake(0, -240 + radius)];
	[space addBody:body];
	
	// shape
	shape = [PMCircleShape circleShapeWithBody:body 
										radius:radius];
	[shape setElasticity:0.0];
	[shape setFriction:0.9];
	[space addShape:shape];
}

/**
 * Returns the demo options to be used for the receiver.
 */
- (PMDemoDrawSettings)drawSettings {
	return PMDemoDrawSettingsDefault;
}

@end
